#pragma once

#include "pch.h"
#include "BasicShapes.h"
#include "Exceptions.h"

#include "Shape.h"

#include <memory>

using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;

Shape::Shape()
{
	this->Name = NULL;
}

Shape::~Shape()
{
	if (this->Name != NULL)
	{
		free(this->Name);
	}

	this->Name = NULL;
}

void Shape::Render(
		_In_ ID3D11DeviceContext1* direct3dContext,
		_In_ ID3D11Buffer* constantBuffer,
		_In_ ID3D11InputLayout* vertexInputLayout,
		_In_ ID3D11VertexShader* vertexShader,
        _In_ ID3D11PixelShader* pixelShader,
		_In_ ID3D11ShaderResourceView* textureSRV,
		_In_ ID3D11SamplerState* textureSampler)
{
	this->Render(
		direct3dContext,
		constantBuffer,
		vertexInputLayout,
		this->VertexStride,
		this->VertexOffset,
		this->IndexCount,
		this->VertexBuffer,
		this->IndexBuffer,
		this->IndexFormat,
		this->VertexTopology,
		vertexShader,
		pixelShader,
		textureSRV,
		textureSampler);
}

///////////////////////////////////////////////////////////////////////////////////////////////
// Renders the given Vertices and Indexes.
//
void Shape::Render(
		_In_ ID3D11DeviceContext1* direct3dContext,
		
		_In_ ID3D11Buffer* constantBuffer,

		_In_ ID3D11InputLayout* vertexInputLayout,
		unsigned int vertexStride,
		unsigned int vertexOffset,
		unsigned int indexCount,
		_In_ ID3D11Buffer* vertexBuffer,
		_In_ ID3D11Buffer* indexBuffer,
		DXGI_FORMAT indexFormat,
		D3D_PRIMITIVE_TOPOLOGY vertexTopology,
		_In_ ID3D11VertexShader* vertexShader,
        _In_ ID3D11PixelShader* pixelShader,
		_In_ ID3D11ShaderResourceView* textureSRV,
		_In_ ID3D11SamplerState* textureSampler)
{
	direct3dContext->IASetInputLayout(vertexInputLayout);

    direct3dContext->IASetVertexBuffers(
        0,                              // starting at the first vertex buffer slot
        1,                              // set one vertex buffer binding
        &vertexBuffer,
        &vertexStride,                        // specify the size in bytes of a single vertex
        &vertexOffset                         // specify the base vertex in the buffer
        );

    // set the index buffer
    direct3dContext->IASetIndexBuffer(
        indexBuffer,
        indexFormat,   // unsigned short index format
        0                       // specify the base index in the buffer
        );

    // specify the way the vertex and index buffers define geometry
    direct3dContext->IASetPrimitiveTopology(vertexTopology);

    // set the vertex shader stage state
    direct3dContext->VSSetShader(
        vertexShader,
        nullptr,                // don't use shader linkage
        0                       // don't use shader linkage
        );

    direct3dContext->VSSetConstantBuffers(
        0,                              // starting at the first constant buffer slot
        1,                              // set one constant buffer binding
        &constantBuffer
        );

    // set the pixel shader stage state
    direct3dContext->PSSetShader(
        pixelShader,
        nullptr,                // don't use shader linkage
        0                       // don't use shader linkage
        );

    direct3dContext->PSSetShaderResources(
        0,                          // starting at the first shader resource slot
        1,                          // set one shader resource binding
        &textureSRV
        );

    direct3dContext->PSSetSamplers(
        0,                          // starting at the first sampler slot
        1,                          // set one sampler binding
        &textureSampler
        );

    // draw the cube
    direct3dContext->DrawIndexed(
        indexCount,     // draw all created vertices
        0,              // starting with the first vertex
        0               // and the first index
        );
}
